import Vue from 'vue'
import VueRouter from 'vue-router'
import store from "@/store/index"
import {Message} from "element-ui";

Vue.use(VueRouter)

//解决当前位置的冗余导航报错
const originalPush = VueRouter.prototype.push

VueRouter.prototype.push = function push(location) {
  return originalPush.call(this, location).catch(err => err)

}

const routes = [
  {
    path:'',
    redirect:'/main'
  },
  {
    path:'/test',
    component:()=>import("../components/content/testUpload")
  },
  {
    path:'/myResume',
    component:()=>import("../views/Resume/index")
  },
  {
    path: '/addResume',
    component:()=>import("../views/Resume/add")
  },
  {
    path:'/other',
    component:()=>import("../views/OtherSpace/index"),
    children:[
      {
        path:'',
        redirect:'archives'
      },
      {
        path:'archives',
        component: ()=>import("../views/OtherSpace/archives")
      },
      {
        path:'dynamic',
        component: ()=>import("../views/OtherSpace/dynamic")
      },
      {
        path:'concern',
        component: ()=>import("../views/OtherSpace/concern")
      },
      {
        path:'collection',
        component: ()=>import("../views/OtherSpace/collection")
      },
      {
        path:'fans',
        component:()=>import("../views/OtherSpace/fans")
      }
    ]
  },
  {
    path:'/personal',
    component:()=>import("../views/PersonalCenter/index"),
    children:[
      {
        path:'',
        redirect:'myArchives'
      },
      {
        path:'myArchives',
        component: ()=>import("../views/PersonalCenter/archives")
      },
      {
        path: 'setting',
        component: ()=>import("../views/PersonalCenter/setting")
      },
      {
        path:'myDynamic',
        component: ()=>import("../views/PersonalCenter/myDynamic")
      },
      {
        path:'myConcern',
        component: ()=>import("../views/PersonalCenter/myConcern")
      },
      {
        path:'myCollection',
        component: ()=>import("../views/PersonalCenter/myCollection")
      },
      {
        path:'myFans',
        component:()=>import("../views/PersonalCenter/myFans")
      }
    ]
  },
  {
    path:'/player',
    component:()=>import("../components/conmmon/Player")
  },

  {
    path:'/management',
    component:()=>import("../views/Management/index"),
    children: [
      {
        path:'company',
        component:()=>import("../views/Management/Company/index"),
        children: [
          {
            path:'/',
            redirect: 'video'
          },
          {
            path:'practice',
            component:()=>import("../views/Management/Company/practice")
          },
          {
            path:'video',
            component:()=>import("../views/Management/Company/video")
          },
          {
            path:'uploadVideo',
            component:()=>import("../views/Management/Company/uploadVideo")
          },
          {
            path:'jobManage',
            component:()=>import("../views/Management/Company/jobManagement")
          },
          {
            path:'postResume',
            component:()=>import("../views/Management/Company/postResume")
          },
          {
            path:'deliversList',
            component:()=>import("../views/Management/Company/deliversList")
          }
        ]
      },
      {
        path:'university',
        component:()=>import("../views/Management/University/index"),
        children: [
          {
            path:'',
            redirect: 'stuList'
          },
          {
            path:"stuList",
            component:()=>import("../views/Management/University/stuList")
          },
          {
            path:'addStu',
            component:()=>import("../views/Management/University/addStu")
          }
        ]
      }
    ]
  },
  {
    path:'/main',
    component:()=>import("../views/Main/Main"),
    children:[
      {
        path:'',
        redirect:'login'
      },
      {
        path:'login',
        component:()=>import("../views/Main/Login")
      },
      {
        path:'rePwd',
        component:()=>import("../views/Main/rePwd")
      },
      {
        path:'register',
        component:()=>import("../views/Main/Register"),
        children: [
          {
            path: "",
            redirect: "company",
          },
          {
            path: "company",
            component: ()=>import("../views/Main/register/Company"),
            meta: {
              title: "企业注册",
            },
          },
          {
            path: "university",
            component: ()=>import("../views/Main/register/School"),
            meta: {
              title: "学校注册",
            },
          },
        ],
      }
    ]
  },
  {
    path:'/home',
    component:()=>import("../views/Home/index"),
    children: [
      {
        path:'',
        redirect: 'recommend'
      },
      {
        path:'recommend',
        component:()=>import("../views/Home/Recommend/index")
      },
      {
        path:'dynamic',
        component:()=>import("../views/Home/Dynamic/index")
      },

      {
        path:'discover',
        component:()=>import("../views/Home/Discover/index")
      },
      {
        path: 'recruitment',
        name:'Recruitment',
        component:()=>import("../views/Home/Recruitment/index")
      },
      {
        path:'userList',
        name:'UserList',
        component:()=>import("@/views/Home/UserList/index")
      },
      {
        path:'recDetails',
        component:()=>import("../views/Home/Recruitment/recdetails")
      },
      {
        path:'deliveryList',
        component:()=>import("../views/Home/Recruitment/deliveryList")
      },
      {
        path:'chatting',
        component:()=>import("../views/Chat/index")
      }
    ]
  }
]

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})

router.beforeEach((to,from,next)=> {
  //路由拦截,拦截未登录的情况进入页面
  if(to.path!=='/main/login'&&to.path!=='/main/register/company'&&to.path!=='/main/register/university'&&to.path!=='/main/rePwd'){
    console.log(to.path)
     if(!store.getters["user/getToken"]){
        next({
          path:'/main/login'
        })
       Message.warning("未登录,先请登录~~")
     }else {
       next()
     }
  }else {
    next()
  }
})

export default router
